package com.szh.zx.gaoDingJavaBook.yitu.three;

public class TestConcurrentError {
	public static void main(String[] args) {
		Student student = new Student("常朝伟", "先生");
		ChangeThread changeThread = new ChangeThread(student);
		PrintThread printThread = new PrintThread(student);
		printThread.start();
		changeThread.start();
	}
}
class Student{
	String name;
	String gender;

	public Student(String name, String gender) {
		this.name = name;
		this.gender = gender;
	}

	@Override
	public String toString() {
		return "Student{" +
				"name='" + name + '\'' +
				", gender='" + gender + '\'' +
				'}';
	}
}
class PrintThread extends Thread{
	Student stu;

	public PrintThread(Student stu) {
		this.stu = stu;
	}

	@Override
	public void run() {
		while (true){
			System.out.println(stu);
		}
	}
}
class ChangeThread extends Thread{
	Student stu;

	public ChangeThread(Student stu) {
		this.stu = stu;
	}

	@Override
	public void run() {
		boolean isOkay = true;
		while (true){
			if (isOkay){
				stu.name = "常朝伟";
				stu.gender = "先生";
			}else {
				stu.name = "张曼玉";
				stu.gender = "女士";
			}
			isOkay = !isOkay;
		}
	}
}
